package myky.com.doctorapp.model;

import com.xiangyao.train.utils.RxScedulerHelper;

import java.io.File;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.Nullable;
import io.reactivex.schedulers.Schedulers;
import myky.com.doctorapp.api.ApiManager;
import myky.com.doctorapp.base.BaseEntity;
import myky.com.doctorapp.contract.PersonInfoContract;
import myky.com.doctorapp.entity.PersionInfoEntity;
import myky.com.doctorapp.entity.SimpleReponseEntity;
import myky.com.doctorapp.entity.UserEntity;
import myky.com.doctorapp.utils.RequestBodyBuilder;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class PersonInfoModel implements PersonInfoContract.Model {
    @Override
    public Observable<BaseEntity<UserEntity.PayLoadBean>> getUserInfo() {
        return ApiManager.getApistore().getUserInfo().compose(RxScedulerHelper.INSTANCE.io_main());
    }

    @Override
    public Observable<BaseEntity<SimpleReponseEntity.PayLoadBean>> updateUserInfo(@Nullable String userName, @Nullable String advanter, String version) {

        RequestBodyBuilder.Builder builder=new RequestBodyBuilder.Builder();


        if(userName!=null){

            builder.params("nickname",userName);
        }
        if(advanter!=null){


            builder.params("headPhoto",advanter);

        }

        RequestBody requestBody = builder.params("version", version).build();


        return ApiManager.getApistore().updateUserInfo(requestBody);
    }

    @Override
    public Observable<BaseEntity<String>> upLoadPic(File file) {
        MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        builder.addFormDataPart("file", file.getName(), RequestBody.create(MEDIA_TYPE_PNG, file));
        MultipartBody.Part requestBody = builder.build().part(0);

        return ApiManager.getApistore().uploadMd5(requestBody).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
    }
}
